package com.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobaibai.dao.CouponBelongDao;
import com.xiaobaibai.entity.Coupon;
import com.xiaobaibai.entity.CouponBelong;
import com.xiaobaibai.service.ICouponBelongService;
import com.xiaobaibai.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2020-10-24
 */
@Service
public class CouponBelongServiceImpl extends ServiceImpl<CouponBelongDao, CouponBelong> implements ICouponBelongService {

    @Autowired
    private ICouponService couponService;

    //获取当前用户已经领取的优惠券ids
    @Override
    @Cacheable(value = "coupon",key = "#root.methodName+'-'+#userId")
    public Set<Integer> getUserHaveCoupon(Integer userId) {
        QueryWrapper<CouponBelong> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("coupon_id");
        queryWrapper.eq("user_id",userId);
        List<CouponBelong> list = this.list(queryWrapper);
        Set<Integer> set=new HashSet<>(list.size());
        list.forEach(l->{
            set.add(l.getCouponId());
        });
        return set;
    }

    //获取当前可用的优惠券
    @Override
    @Cacheable(value = "coupon",key = "#root.methodName+'-'+#userId")
    public List<Coupon> getOkCouponList(Integer userId) {
        QueryWrapper<CouponBelong> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("status",0);
        queryWrapper.select("coupon_id");
        List<CouponBelong> list=this.list(queryWrapper);
        if(list==null||list.size()==0)
            return new ArrayList<>();
        List<Integer> ids=new ArrayList<>();
        list.forEach(l->{
            ids.add(l.getCouponId());
        });
        QueryWrapper<Coupon> q2=new QueryWrapper<>();
        q2.select("id","coupon_name","coupon_type","coupon_price","coupon_threshold","coupon_discount",
                "coupon_count","coupon_invalidation_time");
        q2.in("id",ids);
        return couponService.list(q2);
    }

    //获取当前不可用的优惠券
    @Override
    @Cacheable(value = "coupon",key = "#root.methodName+'-'+#userId")
    public List<Coupon> getFailureCouponList(Integer userId) {
        QueryWrapper<CouponBelong> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("coupon_id");
        queryWrapper.eq("user_id",userId);
        queryWrapper.in("status",1,2);
        List<Integer> ids=new ArrayList<>();
        List<CouponBelong> list=this.list(queryWrapper);
        list.forEach(l->{
            ids.add(l.getCouponId());
        });
        if(ids.size()==0)
            return null;
        QueryWrapper<Coupon> q2=new QueryWrapper<>();
        q2.in("id",ids);
        q2.select("id","coupon_name","coupon_type","coupon_price","coupon_threshold","coupon_discount",
                "coupon_count","coupon_invalidation_time");
        couponService.list(q2);
        return couponService.list(q2);
    }

}
